package io.gitee.devaper.starlight.security.util

import io.gitee.devaper.starlight.common.entity.po.*
import io.gitee.devaper.starlight.security.*
import org.springframework.security.core.Authentication
import org.springframework.security.core.context.SecurityContext
import org.springframework.security.core.userdetails.UserDetails
import java.security.Principal

fun SecurityContext.tryAcquireDetails(): DefaultUserDetails? {
    return authentication.tryAcquireDetails()
}

fun SecurityContext.tryAcquireUser(): UserPO? {
    return tryAcquireDetails()?.user
}

fun Principal?.tryAcquireDetails(): DefaultUserDetails? {
    return (this as Authentication?).tryAcquireDetails()
}

fun Principal?.tryAcquireUser(): UserPO? {
    return tryAcquireDetails()?.user
}

fun Authentication?.tryAcquireDetails(): DefaultUserDetails? {
    return this?.principal as DefaultUserDetails?
}

fun Authentication?.tryAcquireUser(): UserPO? {
    return tryAcquireDetails()?.user
}

fun tryAcquireUserDetails(principal: Any): DefaultUserDetails? {
    return principal as DefaultUserDetails?
}
fun tryAcquireUser(principal: Any): UserPO? {
    return tryAcquireUserDetails(principal)?.user
}